package com.kgc.web;

import com.kgc.dao.DanyuanDao;
import com.kgc.entity.Danyuan;
import com.kgc.utils.PageUtils;
import com.kgc.vo.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "DanyuanServlet", value = "/DanyuanServlet")
public class DanyuanServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opt = request.getParameter("opt");
        PrintWriter writer = response.getWriter();
        DanyuanDao danyuanDao = new DanyuanDao();
        if ("list".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String name = request.getParameter("name");
            PageUtils pageUtils = new PageUtils();
            if (StringUtils.isNotEmpty(page)) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (StringUtils.isNotEmpty(limit)) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            int total = danyuanDao.totalCount(name);
            pageUtils.init(total);
            List<Danyuan> danyuanList = danyuanDao.selectPage(pageUtils, name);
            writer.write(R.success("楼栋列表加载成功..", total, danyuanList).toJSON());
        } else if ("add".equals(opt)) {
            String name = request.getParameter("name");
            String buildingId = request.getParameter("buildingId");
            int count = danyuanDao.addDanyuan(new Danyuan(name, Integer.parseInt(buildingId)));
            System.out.println("count = " + count);
            if (count > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        } else if ("delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = danyuanDao.deleteDanyuan(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        } else if ("edit".equals(opt)) {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String buildingId = request.getParameter("buildingId");
            int count = danyuanDao.updateDanyuan(new Danyuan(Integer.parseInt(id), name, Integer.parseInt(buildingId)));
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
